package com.mycase.im.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mycase.im.boot.entity.database.ImCircle;
import com.mycase.im.boot.entity.database.ImUser;
import com.mycase.im.boot.mapper.CircleMapper;
import com.mycase.im.boot.mapper.UserMapper;
import com.mycase.im.boot.service.CircleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author ZhuHJay
 * @Date 2021/9/16 16:51
 */
@Service
public class CircleServiceImpl extends ServiceImpl<CircleMapper, ImCircle> implements CircleService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<ImCircle> findDynamicListByUserId(ImUser imUser) {
        QueryWrapper<ImCircle> imCircleQueryWrapper = new QueryWrapper<>();
        imCircleQueryWrapper
                .eq("user_id", imUser.getId())
                .orderByDesc("create_time");
        List<ImCircle> list = list(imCircleQueryWrapper);
        list.forEach(imCircle -> imCircle.setImUser(imUser));
        return list;
    }

    @Override
    public ImCircle findDynamicById(Integer id) {
        ImCircle imCircle = baseMapper.selectById(id);
        ImUser user = userMapper.findById(imCircle.getUserId());
        imCircle.setImUser(user);
        return imCircle;
    }

}
